package com.zkthink.ceres.applet.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkthink.base.R;
import com.zkthink.ceres.store.dao.AppletWithdrawMapper;
import com.zkthink.ceres.store.entity.AppletWithdraw;
import com.zkthink.clh.order.dto.ChlPlaceOrderDTO;
import com.zkthink.clh.order.vo.ChlPlaceOrderVO;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.zhl.order.service.ChlOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.server.mapper.AppletStoreServerMapper;
import com.zkthink.ceres.applet.server.domain.entity.AppletStoreServer;
import com.zkthink.ceres.applet.server.service.AppletStoreServerService;

import java.sql.Date;

/**

@Author cmz
@Date 2024-04-30 13:16
*/
@Service
public class AppletStoreServerServiceImpl extends ServiceImpl<AppletStoreServerMapper, AppletStoreServer> implements AppletStoreServerService{
    @Autowired
    private AppletWithdrawMapper appletWithdrawMapper;
    @Autowired
    private ChlOrderService chlOrderService;
    @Override
    public R appletWithdraw(AppletWithdraw appletWithdraw) {
        //添加提现订单
        ChlPlaceOrderDTO chlPlaceOrderDTO = new ChlPlaceOrderDTO();
        chlPlaceOrderDTO.setMid(appletWithdraw.getMid());
        chlPlaceOrderDTO.setCash(appletWithdraw.getCash());
        chlPlaceOrderDTO.setOrderType(6);
//        chlPlaceOrderDTO.setPayTime(new Date());
        chlPlaceOrderDTO.setPayChannel(Integer.valueOf(String.valueOf(appletWithdraw.getAccountType())));
        R<ChlPlaceOrderVO> chlPlaceOrderVOR = chlOrderService.placeOrder(chlPlaceOrderDTO);

        AppletWithdraw mid = appletWithdrawMapper.selectOne(new QueryWrapper<AppletWithdraw>().eq("mid", appletWithdraw.getMid()).eq("applyStatus", 0));
        if (mid != null){
            return R.fail("请勿重复申请");
        }
        appletWithdraw.setMid(BaseContextHandler.getUserId());
        appletWithdraw.setOrderCode(chlPlaceOrderVOR.getData().getOrderCode());
        return appletWithdrawMapper.insert(appletWithdraw) > 0 ? R.success("提现已发起") : R.fail("新增失败");
    }
}
